{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2  九×九乘法口诀表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: jupyter in d:\\computer\\anaconda\\lib\\site-packages (1.0.0)\n",
      "Requirement already satisfied: notebook in d:\\computer\\anaconda\\lib\\site-packages (from jupyter) (7.0.8)\n",
      "Requirement already satisfied: qtconsole in d:\\computer\\anaconda\\lib\\site-packages (from jupyter) (5.5.1)\n",
      "Requirement already satisfied: jupyter-console in d:\\computer\\anaconda\\lib\\site-packages (from jupyter) (6.6.3)\n",
      "Requirement already satisfied: nbconvert in d:\\computer\\anaconda\\lib\\site-packages (from jupyter) (7.10.0)\n",
      "Requirement already satisfied: ipykernel in d:\\computer\\anaconda\\lib\\site-packages (from jupyter) (6.28.0)\n",
      "Requirement already satisfied: ipywidgets in d:\\computer\\anaconda\\lib\\site-packages (from jupyter) (7.8.1)\n",
      "Requirement already satisfied: comm>=0.1.1 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (0.2.1)\n",
      "Requirement already satisfied: debugpy>=1.6.5 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (1.6.7)\n",
      "Requirement already satisfied: ipython>=7.23.1 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (8.25.0)\n",
      "Requirement already satisfied: jupyter-client>=6.1.12 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (8.6.0)\n",
      "Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (5.7.2)\n",
      "Requirement already satisfied: matplotlib-inline>=0.1 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (0.1.6)\n",
      "Requirement already satisfied: nest-asyncio in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (1.6.0)\n",
      "Requirement already satisfied: packaging in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (23.2)\n",
      "Requirement already satisfied: psutil in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (5.9.0)\n",
      "Requirement already satisfied: pyzmq>=24 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (25.1.2)\n",
      "Requirement already satisfied: tornado>=6.1 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (6.4.1)\n",
      "Requirement already satisfied: traitlets>=5.4.0 in d:\\computer\\anaconda\\lib\\site-packages (from ipykernel->jupyter) (5.14.3)\n",
      "Requirement already satisfied: ipython-genutils~=0.2.0 in d:\\computer\\anaconda\\lib\\site-packages (from ipywidgets->jupyter) (0.2.0)\n",
      "Requirement already satisfied: widgetsnbextension~=3.6.6 in d:\\computer\\anaconda\\lib\\site-packages (from ipywidgets->jupyter) (3.6.6)\n",
      "Requirement already satisfied: jupyterlab-widgets<3,>=1.0.0 in d:\\computer\\anaconda\\lib\\site-packages (from ipywidgets->jupyter) (1.0.0)\n",
      "Requirement already satisfied: prompt-toolkit>=3.0.30 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-console->jupyter) (3.0.43)\n",
      "Requirement already satisfied: pygments in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-console->jupyter) (2.15.1)\n",
      "Requirement already satisfied: beautifulsoup4 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (4.12.3)\n",
      "Requirement already satisfied: bleach!=5.0.0 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (4.1.0)\n",
      "Requirement already satisfied: defusedxml in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (0.7.1)\n",
      "Requirement already satisfied: jinja2>=3.0 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (3.1.4)\n",
      "Requirement already satisfied: jupyterlab-pygments in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (0.1.2)\n",
      "Requirement already satisfied: markupsafe>=2.0 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (2.1.3)\n",
      "Requirement already satisfied: mistune<4,>=2.0.3 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (2.0.4)\n",
      "Requirement already satisfied: nbclient>=0.5.0 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (0.8.0)\n",
      "Requirement already satisfied: nbformat>=5.7 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (5.9.2)\n",
      "Requirement already satisfied: pandocfilters>=1.4.1 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (1.5.0)\n",
      "Requirement already satisfied: tinycss2 in d:\\computer\\anaconda\\lib\\site-packages (from nbconvert->jupyter) (1.2.1)\n",
      "Requirement already satisfied: jupyter-server<3,>=2.4.0 in d:\\computer\\anaconda\\lib\\site-packages (from notebook->jupyter) (2.14.1)\n",
      "Requirement already satisfied: jupyterlab-server<3,>=2.22.1 in d:\\computer\\anaconda\\lib\\site-packages (from notebook->jupyter) (2.25.1)\n",
      "Requirement already satisfied: jupyterlab<4.1,>=4.0.2 in d:\\computer\\anaconda\\lib\\site-packages (from notebook->jupyter) (4.0.11)\n",
      "Requirement already satisfied: notebook-shim<0.3,>=0.2 in d:\\computer\\anaconda\\lib\\site-packages (from notebook->jupyter) (0.2.3)\n",
      "Requirement already satisfied: qtpy>=2.4.0 in d:\\computer\\anaconda\\lib\\site-packages (from qtconsole->jupyter) (2.4.1)\n",
      "Requirement already satisfied: six>=1.9.0 in d:\\computer\\anaconda\\lib\\site-packages (from bleach!=5.0.0->nbconvert->jupyter) (1.16.0)\n",
      "Requirement already satisfied: webencodings in d:\\computer\\anaconda\\lib\\site-packages (from bleach!=5.0.0->nbconvert->jupyter) (0.5.1)\n",
      "Requirement already satisfied: decorator in d:\\computer\\anaconda\\lib\\site-packages (from ipython>=7.23.1->ipykernel->jupyter) (5.1.1)\n",
      "Requirement already satisfied: jedi>=0.16 in d:\\computer\\anaconda\\lib\\site-packages (from ipython>=7.23.1->ipykernel->jupyter) (0.18.1)\n",
      "Requirement already satisfied: stack-data in d:\\computer\\anaconda\\lib\\site-packages (from ipython>=7.23.1->ipykernel->jupyter) (0.2.0)\n",
      "Requirement already satisfied: colorama in d:\\computer\\anaconda\\lib\\site-packages (from ipython>=7.23.1->ipykernel->jupyter) (0.4.6)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-client>=6.1.12->ipykernel->jupyter) (2.9.0.post0)\n",
      "Requirement already satisfied: platformdirs>=2.5 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-core!=5.0.*,>=4.12->ipykernel->jupyter) (3.10.0)\n",
      "Requirement already satisfied: pywin32>=300 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-core!=5.0.*,>=4.12->ipykernel->jupyter) (305.1)\n",
      "Requirement already satisfied: anyio>=3.1.0 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (4.2.0)\n",
      "Requirement already satisfied: argon2-cffi>=21.1 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (21.3.0)\n",
      "Requirement already satisfied: jupyter-events>=0.9.0 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (0.10.0)\n",
      "Requirement already satisfied: jupyter-server-terminals>=0.4.4 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (0.4.4)\n",
      "Requirement already satisfied: overrides>=5.0 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (7.4.0)\n",
      "Requirement already satisfied: prometheus-client>=0.9 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (0.14.1)\n",
      "Requirement already satisfied: pywinpty>=2.0.1 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (2.0.10)\n",
      "Requirement already satisfied: send2trash>=1.8.2 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (1.8.2)\n",
      "Requirement already satisfied: terminado>=0.8.3 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (0.17.1)\n",
      "Requirement already satisfied: websocket-client>=1.7 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-server<3,>=2.4.0->notebook->jupyter) (1.8.0)\n",
      "Requirement already satisfied: async-lru>=1.0.0 in d:\\computer\\anaconda\\lib\\site-packages (from jupyterlab<4.1,>=4.0.2->notebook->jupyter) (2.0.4)\n",
      "Requirement already satisfied: jupyter-lsp>=2.0.0 in d:\\computer\\anaconda\\lib\\site-packages (from jupyterlab<4.1,>=4.0.2->notebook->jupyter) (2.2.0)\n",
      "Requirement already satisfied: babel>=2.10 in d:\\computer\\anaconda\\lib\\site-packages (from jupyterlab-server<3,>=2.22.1->notebook->jupyter) (2.11.0)\n",
      "Requirement already satisfied: json5>=0.9.0 in d:\\computer\\anaconda\\lib\\site-packages (from jupyterlab-server<3,>=2.22.1->notebook->jupyter) (0.9.6)\n",
      "Requirement already satisfied: jsonschema>=4.18.0 in d:\\computer\\anaconda\\lib\\site-packages (from jupyterlab-server<3,>=2.22.1->notebook->jupyter) (4.19.2)\n",
      "Requirement already satisfied: requests>=2.31 in d:\\computer\\anaconda\\lib\\site-packages (from jupyterlab-server<3,>=2.22.1->notebook->jupyter) (2.32.2)\n",
      "Requirement already satisfied: fastjsonschema in d:\\computer\\anaconda\\lib\\site-packages (from nbformat>=5.7->nbconvert->jupyter) (2.16.2)\n",
      "Requirement already satisfied: wcwidth in d:\\computer\\anaconda\\lib\\site-packages (from prompt-toolkit>=3.0.30->jupyter-console->jupyter) (0.2.5)\n",
      "Requirement already satisfied: soupsieve>1.2 in d:\\computer\\anaconda\\lib\\site-packages (from beautifulsoup4->nbconvert->jupyter) (2.5)\n",
      "Requirement already satisfied: idna>=2.8 in d:\\computer\\anaconda\\lib\\site-packages (from anyio>=3.1.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (3.7)\n",
      "Requirement already satisfied: sniffio>=1.1 in d:\\computer\\anaconda\\lib\\site-packages (from anyio>=3.1.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (1.3.0)\n",
      "Requirement already satisfied: argon2-cffi-bindings in d:\\computer\\anaconda\\lib\\site-packages (from argon2-cffi>=21.1->jupyter-server<3,>=2.4.0->notebook->jupyter) (21.2.0)\n",
      "Requirement already satisfied: pytz>=2015.7 in d:\\computer\\anaconda\\lib\\site-packages (from babel>=2.10->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (2024.1)\n",
      "Requirement already satisfied: parso<0.9.0,>=0.8.0 in d:\\computer\\anaconda\\lib\\site-packages (from jedi>=0.16->ipython>=7.23.1->ipykernel->jupyter) (0.8.3)\n",
      "Requirement already satisfied: attrs>=22.2.0 in d:\\computer\\anaconda\\lib\\site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (23.1.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in d:\\computer\\anaconda\\lib\\site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (2023.7.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in d:\\computer\\anaconda\\lib\\site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (0.30.2)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in d:\\computer\\anaconda\\lib\\site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (0.10.6)\n",
      "Requirement already satisfied: python-json-logger>=2.0.4 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (2.0.7)\n",
      "Requirement already satisfied: pyyaml>=5.3 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (6.0.1)\n",
      "Requirement already satisfied: rfc3339-validator in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (0.1.4)\n",
      "Requirement already satisfied: rfc3986-validator>=0.1.1 in d:\\computer\\anaconda\\lib\\site-packages (from jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (0.1.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in d:\\computer\\anaconda\\lib\\site-packages (from requests>=2.31->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (2.0.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\computer\\anaconda\\lib\\site-packages (from requests>=2.31->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (2.2.2)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\computer\\anaconda\\lib\\site-packages (from requests>=2.31->jupyterlab-server<3,>=2.22.1->notebook->jupyter) (2024.7.4)\n",
      "Requirement already satisfied: executing in d:\\computer\\anaconda\\lib\\site-packages (from stack-data->ipython>=7.23.1->ipykernel->jupyter) (0.8.3)\n",
      "Requirement already satisfied: asttokens in d:\\computer\\anaconda\\lib\\site-packages (from stack-data->ipython>=7.23.1->ipykernel->jupyter) (2.0.5)\n",
      "Requirement already satisfied: pure-eval in d:\\computer\\anaconda\\lib\\site-packages (from stack-data->ipython>=7.23.1->ipykernel->jupyter) (0.2.2)\n",
      "Collecting fqdn (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter)\n",
      "  Downloading fqdn-1.5.1-py3-none-any.whl.metadata (1.4 kB)\n",
      "Collecting isoduration (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter)\n",
      "  Downloading isoduration-20.11.0-py3-none-any.whl.metadata (5.7 kB)\n",
      "Requirement already satisfied: jsonpointer>1.13 in d:\\computer\\anaconda\\lib\\site-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (2.1)\n",
      "Collecting uri-template (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter)\n",
      "  Downloading uri_template-1.3.0-py3-none-any.whl.metadata (8.8 kB)\n",
      "Collecting webcolors>=1.11 (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter)\n",
      "  Downloading webcolors-24.11.1-py3-none-any.whl.metadata (2.2 kB)\n",
      "Requirement already satisfied: cffi>=1.0.1 in d:\\computer\\anaconda\\lib\\site-packages (from argon2-cffi-bindings->argon2-cffi>=21.1->jupyter-server<3,>=2.4.0->notebook->jupyter) (1.16.0)\n",
      "Requirement already satisfied: pycparser in d:\\computer\\anaconda\\lib\\site-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi>=21.1->jupyter-server<3,>=2.4.0->notebook->jupyter) (2.21)\n",
      "Requirement already satisfied: arrow>=0.15.0 in d:\\computer\\anaconda\\lib\\site-packages (from isoduration->jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.9.0->jupyter-server<3,>=2.4.0->notebook->jupyter) (1.2.3)\n",
      "Downloading webcolors-24.11.1-py3-none-any.whl (14 kB)\n",
      "Downloading fqdn-1.5.1-py3-none-any.whl (9.1 kB)\n",
      "Downloading isoduration-20.11.0-py3-none-any.whl (11 kB)\n",
      "Downloading uri_template-1.3.0-py3-none-any.whl (11 kB)\n",
      "Installing collected packages: webcolors, uri-template, fqdn, isoduration\n",
      "Successfully installed fqdn-1.5.1 isoduration-20.11.0 uri-template-1.3.0 webcolors-24.11.1\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install jupyter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem1():\n",
    "    def print_multiplication_table():\n",
    "        for i in range(1, 10):\n",
    "            for j in range(1, i + 1):\n",
    "                print(f\"{j}*{i}={i * j}\", end=\"\\t\")\n",
    "            print()  \n",
    "    print_multiplication_table()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4  输入任意正整数，输出分解质因数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem2():\n",
    "    def prime_factors(n):\n",
    "        factors = []\n",
    "        while n % 2 == 0:\n",
    "            factors.append(2)\n",
    "            n = n // 2\n",
    "        for i in range(3, int(n**0.5) + 1, 2):\n",
    "            while n % i == 0:\n",
    "                factors.append(i)\n",
    "                n = n // i\n",
    "        if n > 2:\n",
    "            factors.append(n)\n",
    "        return factors\n",
    "    num = int(input(\"请输入一个正整数: \"))\n",
    "    print(f\"{num} 的质因数分解是: {prime_factors(num)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6  输入一个不多于6位的正整数，要求：求它是几位数，逆序打印出各位数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem3():\n",
    "    def main():\n",
    "      num_str = input(\"请输入一个不多于6位的正整数: \")\n",
    "      if num_str.isdigit() and 0 < int(num_str) <= 999999:\n",
    "          num_digits = len(num_str)\n",
    "          reversed_num_str = num_str[::-1]\n",
    "          print(f\"这个数是{num_digits}位数。\")\n",
    "          print(f\"逆序打印各位数字为: {reversed_num_str}\")\n",
    "      else:\n",
    "          print(\"输入无效，请输入一个不多于6位的正整数。\")\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8 求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem4():\n",
    "    def calculate_series_sum(a, n):\n",
    "        total_sum = 0\n",
    "        current_term = 0 \n",
    "        for i in range(1, n + 1): \n",
    "            current_term = current_term * 10 + a\n",
    "            total_sum += current_term\n",
    "        return total_sum\n",
    "        a = int(input(\"请输入一个数字a: \"))\n",
    "    n = int(input(\"请输入相加的项数n: \"))\n",
    "    result = calculate_series_sum(a, n)\n",
    "    print(f\"表达式 s = {a} + {a*10+a} + {a*100+a*10+a} + ... (共{n}项) 的值是: {result}\")\n",
    "    print(f\"实际的计算结果是: {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem5():\n",
    "    from math import gcd\n",
    "\n",
    "    def fraction_sum(n):\n",
    "        num1, den1 = 2, 1\n",
    "        num2, den2 = 3, 2\n",
    "        total_num = 0\n",
    "        total_den = 1\n",
    "        for _ in range(n):\n",
    "            current_num = num1\n",
    "            current_den = den1\n",
    "            total_num = total_num * current_den + current_num * total_den\n",
    "            total_den *= current_den\n",
    "            num1, den1 = num2, den2\n",
    "            num2, den2 = num1 + num2, den1 + den2\n",
    "        common_divisor = gcd(total_num, total_den)\n",
    "        simplified_num = total_num // common_divisor\n",
    "        simplified_den = total_den // common_divisor\n",
    "        return simplified_num, simplified_den\n",
    "\n",
    "    n = int(input(\"请输入要计算的项数 N: \"))\n",
    "    result_num, result_den = fraction_sum(n)\n",
    "    print(f\"数列的前{n}项之和是: {result_num}/{result_den}\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12  一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem6():\n",
    "    def find_x():\n",
    "        factors = [(1, 268), (2, 134), (4, 67), (134, 2), (67, 4), (268, 1)]\n",
    "        for factor1, factor2 in factors:\n",
    "            if factor1 > factor2:\n",
    "                factor1, factor2 = factor2, factor1\n",
    "            m = (factor1 + factor2) // 2\n",
    "            n = (factor2 - factor1) // 2\n",
    "            if m ** 2 - 100 >= 0:\n",
    "                x = m ** 2 - 100\n",
    "                if int((x + 368) ** 0.5) ** 2 == x + 368:\n",
    "                    return x\n",
    "        return None\n",
    "\n",
    "    x = find_x()\n",
    "    print(f\"满足条件的整数是: {x}\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14  有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem7():\n",
    "    def calculate_rabbit_populations(months):\n",
    "        populations = [0] * (months + 1)\n",
    "        if months >= 1:\n",
    "            populations[1] = 1\n",
    "        if months >= 2:\n",
    "            populations[2] = 1\n",
    "        for month in range(3, months + 1):\n",
    "            populations[month] = populations[month - 1] + populations[month - 2]\n",
    "        return populations[1:]\n",
    "\n",
    "    months = int(input(\"请输入要计算的月份数: \"))\n",
    "    populations = calculate_rabbit_populations(months)\n",
    "    for month, population in enumerate(populations, start = 1):\n",
    "        print(f\"第{month}个月的兔子总数为: {population}对\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16  任意输入一个3x3矩阵，求矩阵对角线元素之和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem8():\n",
    "    def input_matrix():\n",
    "        matrix = []\n",
    "        print(\"请输入一个3x3的矩阵，每行输入3个数字，用空格分隔:\")\n",
    "        for i in range(3):\n",
    "            row = list(map(int, input().split()))\n",
    "            if len(row)!= 3:\n",
    "                print(f\"第{i + 1}行应该输入3个数字，请重新输入。\")\n",
    "                i -= 1\n",
    "                continue\n",
    "            matrix.append(row)\n",
    "        return matrix\n",
    "\n",
    "    def diagonal_sum(matrix):\n",
    "        main_diagonal_sum = 0\n",
    "        secondary_diagonal_sum = 0\n",
    "        for i in range(3):\n",
    "            main_diagonal_sum += matrix[i][i]\n",
    "        for i in range(3):\n",
    "            secondary_diagonal_sum += matrix[i][2 - i]\n",
    "        return main_diagonal_sum, secondary_diagonal_sum\n",
    "\n",
    "    matrix = input_matrix()\n",
    "    main_sum, secondary_sum = diagonal_sum(matrix)\n",
    "    print(f\"主对角线元素之和为: {main_sum}\")\n",
    "    print(f\"副对角线元素之和为: {secondary_sum}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18  有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem9():\n",
    "    def rotate_list(nums, m):\n",
    "        if not nums or m == 0:\n",
    "            return nums\n",
    "        n = len(nums)\n",
    "        m = m % n\n",
    "        rotated_nums = nums[-m:] + nums[:-m]\n",
    "        return rotated_nums\n",
    "\n",
    "    nums = [1, 2, 3, 4, 5, 6, 7]\n",
    "    m = 3\n",
    "    rotated = rotate_list(nums, m)\n",
    "    print(rotated)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "20  编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [],
   "source": [
    "def problem10():\n",
    "    def sum_even_series(n):\n",
    "        \"\"\"计算偶数项数列的和: 1/2 + 1/4 +... + 1/n\"\"\"\n",
    "        return sum(1 / i for i in range(2, n + 1, 2))\n",
    "\n",
    "    def sum_odd_series(n):\n",
    "        \"\"\"计算奇数项数列的和: 1/1 + 1/3 +... + 1/n\"\"\"\n",
    "        return sum(1 / i for i in range(1, n + 1, 2))\n",
    "\n",
    "    def calculate_series_sum(n):\n",
    "        \"\"\"根据n的奇偶性调用相应的函数来计算数列和\"\"\"\n",
    "        if n % 2 == 0:\n",
    "            return sum_even_series(n)\n",
    "        else:\n",
    "            return sum_odd_series(n)\n",
    "\n",
    "    n = int(input(\"请输入一个正整数n: \"))\n",
    "    result = calculate_series_sum(n)\n",
    "    print(f\"数列和为: {result}\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "无效的输入，请输入1、2或3。\n"
     ]
    }
   ],
   "source": [
    "user_choice = input(\"请输入您的选择：\")\n",
    "if user_choice == '1':\n",
    "    problem1()\n",
    "elif user_choice == '2':\n",
    "    problem2()\n",
    "elif user_choice == '3':\n",
    "    problem3()\n",
    "elif user_choice == '4':\n",
    "    problem4()\n",
    "elif user_choice == '5':\n",
    "    problem5()\n",
    "elif user_choice == '6':\n",
    "    problem6()\n",
    "elif user_choice == '7':\n",
    "    problem7()\n",
    "elif user_choice == '8':\n",
    "    problem8()\n",
    "elif user_choice == '9':\n",
    "    problem9()\n",
    "elif user_choice == '10':\n",
    "    problem10()\n",
    "else:\n",
    "    print(\"无效的输入，请输入1、2或3。\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
